package com.hippy.lib.utils.view

import android.content.Context
import android.graphics.Typeface
import android.text.TextUtils
import android.util.TypedValue
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import com.google.android.material.appbar.CollapsingToolbarLayout
import com.hippy.lib.utils.ConvertUtils

object ViewUtils {
    fun createLinearLayout(context: Context, isVertical: Boolean = true): LinearLayout {
        val linearLayout = LinearLayout(context)
        linearLayout.orientation =
            if (isVertical) LinearLayout.VERTICAL else LinearLayout.HORIZONTAL
        return linearLayout
    }
}

fun View.hiLayoutParams(
    widthDipValue: Float,
    heightDipValue: Float
) {
    var width = -1
    if (widthDipValue <= ViewGroup.LayoutParams.WRAP_CONTENT) {
        width = ViewGroup.LayoutParams.WRAP_CONTENT
    } else if (widthDipValue > 0) {
        width =
            ConvertUtils.dp2px(widthDipValue)
    }

    var height = -1
    if (heightDipValue <= ViewGroup.LayoutParams.WRAP_CONTENT) {
        height = ViewGroup.LayoutParams.WRAP_CONTENT
    } else if (heightDipValue > 0) {
        height =
            ConvertUtils.dp2px(heightDipValue)
    }
    this.layoutParams = ViewGroup.LayoutParams(width, height)
}

fun View.hiLayoutParams(
    widthDipValue: Int,
    heightDipValue: Int
) {
    hiLayoutParams(widthDipValue.toFloat(), heightDipValue.toFloat())
}

fun ViewGroup.hiAddView(
    view: View,
    widthDipValue: Float,
    heightDipValue: Float
) {
    var width = -1
    if (widthDipValue <= ViewGroup.LayoutParams.WRAP_CONTENT) {
        width = ViewGroup.LayoutParams.WRAP_CONTENT
    } else if (widthDipValue > 0) {
        width = ConvertUtils.dp2px(widthDipValue)
    }

    var height = -1
    if (heightDipValue <= ViewGroup.LayoutParams.WRAP_CONTENT) {
        height = ViewGroup.LayoutParams.WRAP_CONTENT
    } else if (heightDipValue > 0) {
        height = ConvertUtils.dp2px(heightDipValue)
    }
    when (this) {
        is LinearLayout -> {
            view.layoutParams = LinearLayout.LayoutParams(width, height)
        }
        is FrameLayout -> {
            view.layoutParams = FrameLayout.LayoutParams(width, height)
        }
        is RelativeLayout -> {
            view.layoutParams = RelativeLayout.LayoutParams(width, height)
        }
        is CollapsingToolbarLayout -> {
            view.layoutParams = CollapsingToolbarLayout.LayoutParams(width, height)
        }
        else -> {
            view.layoutParams = ViewGroup.LayoutParams(width, height)
        }
    }
    this.addView(view)
}

fun ViewGroup.hiAddView(
    view: View,
    widthDipValue: Int,
    heightDipValue: Int
) {
    hiAddView(view, widthDipValue.toFloat(), heightDipValue.toFloat())
}

fun View.hiPadding(
    leftDipValue: Float,
    topDipValue: Float = leftDipValue,
    rightDipValue: Float = leftDipValue,
    bottomDipValue: Float = topDipValue
) {
    this.setPadding(
        ConvertUtils.dp2px(leftDipValue),
        ConvertUtils.dp2px(topDipValue),
        ConvertUtils.dp2px(rightDipValue),
        ConvertUtils.dp2px(bottomDipValue)
    )
}

fun View.hiPaddingLeft(
    leftDipValue: Float
) {
    this.setPadding(
        ConvertUtils.dp2px(leftDipValue),
        this.paddingTop,
        this.paddingRight,
        this.paddingBottom
    )
}

fun View.hiMargin(
    leftDipValue: Float,
    topDipValue: Float = leftDipValue,
    rightDipValue: Float = leftDipValue,
    bottomDipValue: Float = topDipValue
) {
    this.layoutParams?.let {
        if (it is ViewGroup.MarginLayoutParams) {
            it.leftMargin = ConvertUtils.dp2px(leftDipValue)
            it.topMargin = ConvertUtils.dp2px(topDipValue)
            it.rightMargin = ConvertUtils.dp2px(rightDipValue)
            it.bottomMargin = ConvertUtils.dp2px(bottomDipValue)
        }
    }
}

fun View.hiMarginLeft(leftDipValue: Float) {
    this.layoutParams?.let {
        if (it is ViewGroup.MarginLayoutParams) {
            it.leftMargin = ConvertUtils.dp2px(leftDipValue)
        }
    }
}

fun View.hiMarginTop(topDipValue: Float) {
    this.layoutParams?.let {
        if (it is ViewGroup.MarginLayoutParams) {
            it.topMargin = ConvertUtils.dp2px(topDipValue)
        }
    }
}

fun View.hiMarginRight(rightDipValue: Float) {
    this.layoutParams?.let {
        if (it is ViewGroup.MarginLayoutParams) {
            it.rightMargin = ConvertUtils.dp2px(rightDipValue)
        }
    }
}

fun View.hiMarginBottom(bottomDipValue: Float) {
    this.layoutParams?.let {
        if (it is ViewGroup.MarginLayoutParams) {
            it.bottomMargin = ConvertUtils.dp2px(bottomDipValue)
        }
    }
}

fun View.hiGravity(gravity: Int) {
    when (this) {
        is TextView -> {
            this.gravity = gravity
        }
        is LinearLayout -> {
            this.gravity = gravity
        }
    }
}

fun View.hiLayoutGravity(gravity: Int) {
    val layoutParams = this.layoutParams
    layoutParams?.let {
        when (it) {
            is LinearLayout.LayoutParams -> {
                it.gravity = gravity
            }
            is FrameLayout.LayoutParams -> {
                it.gravity = gravity
            }
        }
    }
}

fun TextView.hiSetTextSize(spValue: Float) {
    this.setTextSize(TypedValue.COMPLEX_UNIT_DIP, spValue)
}

fun TextView.hiEllipsizeEnd() {
    this.ellipsize = TextUtils.TruncateAt.END
}

fun TextView.hiTypefaceBold() {
    this.typeface = Typeface.DEFAULT_BOLD
}

fun TextView.hiTypefaceDefault() {
    this.typeface = Typeface.DEFAULT
}

fun ViewGroup.LayoutParams.hiWeight(weightValue: Float) {
    when(this){
        is LinearLayout.LayoutParams -> this.weight = weightValue
    }
}

